import MonthCalculator from './month-calculator.min';

Component({
    properties: {
        /* 是否显示 */
        show: {
            type: Boolean,
            value: false
        },
        /* 显示第几年，从开始日期为第一年 */
        current: {
            type: Number,
            value: 0
        },
        /* 选择的日期 */
        currentMonth: {
            type: String,
            value: ''
        },
        /* 开始日期 */
        startYear: {
            type: String,
            value: '',
            observer(newVal, oldVal) {
                this._getTotalYear();
            }
        },
        /* 结束日期 */
        endYear: {
            type: String,
            value: '',
            observer(newVal, oldVal) {
                this._getTotalYear();
            }
        },
    },
    ready() {
        this._getTotalYear();
        this._setSwiperHeight();
    },
    catchEvent: function(event) {

    },
    methods: {
        change(event) {
            let current = event.detail.current;

            this.setData({
                current: current
            });

            this._setSwiperHeight();
        },
        changeToPrev(event) {
            let {
                current
            } = this.data;

            current--;

            if (current < 0) current = 0;

            this.setData({
                current: current
            });
        },
        changeToNext(event) {
            let {
                current,
                years
            } = this.data;

            current++;

            if (current >= years.length) current = years.length - 1;

            this.setData({
                current: current
            });
        },
        changeToToday(event) {
            let {
                current,
                currentMonth,
                todayMonthIndex,
                todayDate
            } = this.data;

            this.setData({
                current: todayMonthIndex,
                currentDate: todayDate
            });

            this.triggerEvent('change', {
                currentDate: todayDate
            });
        },
        tapMask() {
            this.setData({
                show: false
            });
        },
        tapMonth(event) {
            let that = this;
            let {
                current,
                currentMonth,
                todayMonthIndex,
                todayDate
            } = this.data;
            let dataset = event.target.dataset;

            if (dataset.disabled) return;

            this.setData({
                currentMonth: dataset.datestr
            });

            setTimeout(function() {
                //要延时执行的代码
                that.triggerEvent('change', {
                    currentMonth: dataset.datestr
                });
            }, 10)


        },
        formatNumber(n) {
            n = n.toString()
            return n[1] ? n : '0' + n
        },
        _getTotalYear() {
            let {
                startYear,
                endYear
            } = this.properties;
            let oMonth = new MonthCalculator({
                startYear: startYear,
                endYear: endYear
            });
            let nowDate = new Date();
            let _currentMonth = nowDate.getFullYear() + '-' + this.formatNumber(nowDate.getMonth() + 1);
            this.properties.current = nowDate.getFullYear() - startYear;
            this.setData({
                current: nowDate.getFullYear() - startYear,
                currentMonth: _currentMonth
            });
            let years = oMonth.getTotalYear();
            let enableToday = oMonth.getToday();
            let data = {};

            if (enableToday) {
                data.showTodayBtn = true;
                data.todayMonthIndex = enableToday.monthIndex;
                data.todayDate = enableToday.date;
            }

            data.years = years;

            this.setData(data);
        },
        _setSwiperHeight() {
            wx.createSelectorQuery().in(this).selectAll(`.weapp-calendar-picker-month`).boundingClientRect((rects) => {
                let {
                    current
                } = this.data;

                let rect = rects[current];

                if (!rect || !rect.height) return;

                let swiperHeight = rect.height + 'px';

                this.setData({
                    swiperHeight: swiperHeight
                });
            }).exec();
        }
    }
})